/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof_trab1;

import java.lang.management.ManagementFactory;
import com.sun.management.OperatingSystemMXBean;

public class ParsersController extends Thread {

    private static ParsersController pc = new ParsersController();
    private static final int WAIT_TIME = 1000;

    private ParsersController() {
    }

    public static ParsersController getInstance() {
        return pc;
    }

    @Override
    public void run() {
        ParsersLauncher pl = ParsersLauncher.getInstance();
        OperatingSystemMXBean mx = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        double cpuUsage, maxCpuUsage;

        try {
            while (!isInterrupted()) {
                maxCpuUsage = pl.getMaxCpuUsage();
                cpuUsage = mx.getSystemCpuLoad();
                if (cpuUsage < maxCpuUsage) {
                    pl.increaseParsers();
                } else {
                    pl.decreaseParsers();
                }

                Thread.sleep(WAIT_TIME);
            }
        } catch (InterruptedException ex) {
        }
    }
}
